// pages/clickMe/plan/comment.js
import { API_HOST_1 } from '../../../api/config'
import api from '../../../api/index'
import { userInfoStore } from '../../../store/userInfo'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLevel: 0,
    contractId: 0,
    saleUserId: 0,
    customerName:'',
    customerPhone:'',
    images: [],
    uploads: [],
    comment:'',
    attach:[],
    proposalStars: {
      starSize: 'large',
      score: 0,
      starIdx: 'I',
      gradable: true
    },
    dealStars: {
      starSize: 'large',
      score: 0,
      starIdx: 'II',
      gradable: true
    },
    efficiencyStars: {
      starSize: 'large',
      score: 0,
      starIdx: 'III',
      gradable: true
    },
    constructionStars:{
      starSize: 'large',
      score: 0,
      starIdx: 'IIII',
      gradable: true
    },
    serviceStars:{
        starSize: 'large',
        score: 0,
        starIdx: 'IIIII',
        gradable: true
    },
    haveComment:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(
      {
        userLevel: userInfoStore.userInfo.level,
        contractId: options.id,
        saleUserId: options.saleUserId,
        customerName: options.n,
        customerPhone: options.p,
      });
    let self = this
    api.geCommentByContract({ contractId: options.id}).then(res => {
        if(res.result){
          self.setData({
            proposalStars: Object.assign(self.data.proposalStars, { score: res.result.review.proposalStars }),
            dealStars: Object.assign(self.data.dealStars, { score: res.result.review.dealStars }),
            efficiencyStars: Object.assign(self.data.efficiencyStars, { score: res.result.review.efficiencyStars }),
            constructionStars: Object.assign(self.data.constructionStars, { score: res.result.review.constructionStars }),
            serviceStars: Object.assign(self.data.serviceStars, { score: res.result.review.serviceStars }),
            comment: res.result.review.review,
            attach: res.result.attach,
            haveComment: true,
          })
        }
    })
  },

  onShow: function(){
    let self=this
    

  },

  getscoreI(e) {
    this.data.proposalStars.score = e.detail.score;
  },
  getscoreII(e) {
    this.data.dealStars.score = e.detail.score;
  },
  getscoreIII(e) {
    this.data.efficiencyStars.score = e.detail.score;
  },
  getscoreIIII(e) {
    this.data.constructionStars.score = e.detail.score;
  },
  getscoreIIIII(e) {
    this.data.serviceStars.score = e.detail.score;
  },

  bindKeyCommentInput: function (e) {
    //console.log(e.detail.value)
    this.setData({
      comment: e.detail.value.replace(/\s+/g, '')
    });
  },

  chooseImage: function () {
    let self = this
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // console.log(JSON.stringify(res))
        wx.showLoading()
        var tempFilePaths = res.tempFilePaths
        let count = 0
        tempFilePaths.forEach((image, index) => {
          wx.uploadFile({
            url: API_HOST_1 + 'daikin/upload/add',
            filePath: image,
            name: 'image',
            success(res) {
              if (res.statusCode == 200) {
                let data = JSON.parse(res.data);
                self.data.uploads.push({ type:10, uri: data.result });
                self.data.images.push({ uri: image })
                self.setData({
                  images: self.data.images,
                  uploads: self.data.uploads
                })
              }
            },
            complete() {
              count++;
              if (tempFilePaths.length == count) {
                wx.hideLoading()
              }
            }
          })
        });
      }
    })
  },

  delImage: function (e) {
    let idx = e.currentTarget.dataset.index;
    this.data.images.splice(idx, 1)
    this.data.uploads.splice(idx, 1)
    this.setData({
      images: this.data.images,
      uploads: this.data.uploads
    })
  },

  sumbit:function(){
    let check = true;
    let msg = "";
    if (this.data.uploads.length == 0) {
      check = false;
      msg = "请上传图片";
    }
    if (this.data.comment.length == 0) {
      check = false;
      msg = "请填写评价内容";
    }
    if(this.data.serviceStars.score==0){
      check=false;
      msg="请对服务态度评价";
    }
    if(this.data.constructionStars.score==0){
      check = false;
      msg = "请对施工规范评价";
    }
    if(this.data.efficiencyStars.score==0){
      check = false;
      msg = "请对配货速度评价";
    }
    if (this.data.dealStars.score==0){
      check = false;
      msg = "请对交易过程规范性评价";
    }
    if(this.data.proposalStars.score==0){
      check = false;
      msg = "请对方案合理性评价";
    }
    
    if(check==false){
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    let params = {
      review: {
        contractId: this.data.contractId,
        proposalStars: this.data.proposalStars.score,
        dealStars: this.data.dealStars.score,
        efficiencyStars: this.data.efficiencyStars.score,
        constructionStars: this.data.constructionStars.score,
        serviceStars: this.data.serviceStars.score,
        review: this.data.comment
      },
      reviewAttachments: this.data.uploads
    }
    api.saveCommentByContract(params).then(res => {
      wx.showModal({
        title: '提示',
        content: '提交成功!',
      })
    },err => {
      
    })


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})